﻿using System;

namespace Myotragus.Data.Tuples
{
	public class Tuple<TObject> : IEquatable<TObject>
		where TObject : class
	{
		#region Constants

		private static readonly Func<TObject, int> GetHashCodeMethod = EqualityFunctionsGenerator<TObject>
			.CreateGetHashCode();
		private static readonly Func<TObject, TObject, bool> EqualsMethod = EqualityFunctionsGenerator<TObject>
			.CreateEqualityComparer();

		#endregion Constants

		#region object Overrides

		public override bool Equals(object obj)
		{
			return Equals(obj as TObject);
		}

		public override int GetHashCode()
		{
			var @this = ((object)this) as TObject;

			return @this == null ? 0 : GetHashCodeMethod(@this);
		}

		#endregion object Overrides

		#region IEquatable<TEntity> Members

		public bool Equals(TObject other)
		{
			var @this = ((object)this) as TObject;

			if (other == null || @this == null)
				return false;

			return EqualsMethod(@this, other);
		}

		#endregion IEquatable<TEntity> Members
	}
}